//
//  shoppingCartCell.m
//  OrderMeal
//
//  Created by 宏创 on 14-3-10.
//  Copyright (c) 2014年 周浩. All rights reserved.
//

#import "shoppingCartCell.h"
#import "CustomIOS7AlertView.h"
#import "ShoppingCartViewController.h"
UITextField *numTextField;
@interface shoppingCartCell()

@end
@implementation shoppingCartCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
    }
    return self;
}

- (IBAction)changeNum:(UIButton *)sender {
    CustomIOS7AlertView *alert = [[CustomIOS7AlertView alloc]init];
    UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    UIButton *minusBtn = [[UIButton alloc]initWithFrame:CGRectMake(24, 25, 45, 45)];
    [minusBtn setTitle:@"-" forState:UIControlStateNormal];
    [minusBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [minusBtn.layer setBorderColor:[[UIColor blackColor]CGColor]];
    [minusBtn.layer setBorderWidth:1.0];
    [minusBtn addTarget:self action:@selector(numMinus) forControlEvents:UIControlEventTouchUpInside];
    
    numTextField = [[UITextField alloc]initWithFrame:CGRectMake(71, 25, 60, 45)];
    [numTextField setText:self.numBtn.currentTitle];
    [numTextField setTextAlignment:NSTextAlignmentCenter];
    [numTextField.layer setBorderWidth:1.0];
    [numTextField.layer setBorderColor:[[UIColor blackColor] CGColor]];
    
    UIButton *addBtn = [[UIButton alloc]initWithFrame:CGRectMake(133, 25, 45, 45)];
    [addBtn setTitle:@"+" forState:UIControlStateNormal];
    [addBtn.layer setBorderColor:[[UIColor blackColor]CGColor]];
    [addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(numAdd) forControlEvents:UIControlEventTouchUpInside];

    [addBtn.layer setBorderWidth:1.0];

    [demoView addSubview:minusBtn];
    [demoView addSubview:addBtn];
    [demoView addSubview:numTextField];
    [alert setContainerView:demoView];
    [alert setButtonTitles:@[@"确认",@"取消"]];
    [alert setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex) {
        if (buttonIndex == 0 ) {
            if (![numTextField.text isEqualToString:[sender currentTitle]]) {
                [sender setTitle:numTextField.text forState:UIControlStateNormal];
                int num = [[self.numBtn currentTitle]intValue];
                float salePrice = [[self.salesPrice.text substringFromIndex:1] floatValue];
                self.cellTotalPrice = [NSString stringWithFormat:@"%.2f",num*salePrice];            NSLog(@"currentCellTotalPrice-->%@",self.cellTotalPrice);
                [[NSNotificationCenter defaultCenter]postNotificationName:@"cellTotalPrice Changed" object:nil];
            }
        }
        if (buttonIndex == 1 ) {
            
        }
        [alertView close];
    }];
            [alert show];
}

-(void)numMinus{
    int _number = [numTextField.text intValue];
    if (_number == 1) {
        return;
    }
    [numTextField setText:[NSString stringWithFormat:@"%d",--_number]];
}

-(void)numAdd{
    int _number = [numTextField.text intValue];
    [numTextField setText:[NSString stringWithFormat:@"%d",++_number]];
}
@end
